package com.greenline.search.test.test;

import com.greenline.search.test.dao.DiseaseAliasDao;
import com.greenline.search.test.dao.UserObject;
import com.greenline.search.test.dao.DepartmentDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-11
 * Time: 下午4:27
 * To change this template use File | Settings | File Templates.
 */
public class GetDiseaseAlias {
    private static Logger logger = LoggerFactory.getLogger(GetDiseaseAlias.class);

    public static void main(String[] args) {
        String concatStr = "&&&&&&&";
        DiseaseAliasDao diseaseAliasDao = new DiseaseAliasDao();
        List<UserObject> userObjectList = diseaseAliasDao.getAllDiseaseAndAlias();
        Map<UserObject, List<String>> diseaseListMap = new HashMap<UserObject, List<String>>();
        //format department name and alias to map
        for(UserObject userObject : userObjectList){
            List<String> nameList = new ArrayList<String>();
            Collections.addAll(nameList, userObject.getName().split(","));
            Collections.addAll(nameList, userObject.getAlias().split(","));
            diseaseListMap.put(userObject, nameList);
        }
        System.out.println("The disease list size is " + userObjectList.size());
        StringBuffer sb = new StringBuffer();
        //print the appropriate name and alias
        for(List<String> nameList : diseaseListMap.values()){

            //标志相同的词
            for(int i = 0; i < nameList.size(); i++){
                for(int j = i + 1; j < nameList.size(); j++){
                    if(nameList.get(i).equals(nameList.get(j)) && !nameList.get(j).contains(concatStr)){
                        nameList.set(j, nameList.get(j)+concatStr);
                    }
                }
            }
            //remove duplicate disease name
            Iterator<String> iterator = nameList.iterator();
            while (iterator.hasNext()){
                String name = iterator.next();
                if (name.contains(concatStr)){
                    iterator.remove();
                }
            }

            for(int i = 0; i < nameList.size(); i++){
                if(nameList.size() == 1){
                    continue;
                }
                sb.append(nameList.get(i));
                if(i != nameList.size()-1){
                    sb.append(",");
                }
            }


            if(nameList.size() > 1){
                sb.append("\n");
            }

        }
        logger.info(sb.toString());
    }

}
